package com.grt.common.util;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.grt.common.WebSecurityConfig;
import com.grt.user.domain.User;
import com.grt.user.service.UserService;

/**
 * @author recivejt 用户工具类
 */
@Component
public class UserUtil {

	private static UserService userService;

	@Autowired
	private UserService aUserService;

	@PostConstruct
	public void init() {
		userService = aUserService;
	}

	public static User getLoginUser() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		HttpSession session = request.getSession();
		if (null == session) {
			return null;
		}
		String username = (String) session.getAttribute(WebSecurityConfig.SESSION_KEY);
		if (null == username) {
			return null;
		} else {
			User oUser = userService.getUser((String) username);
			return oUser;
		}
	}
}
